/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 /** @author Marek Brun */
package com.googlecode.milib.dbg {
	import com.googlecode.milib.MILibMain;
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.service.singleton.KeyboardService;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.utils.getTimer;

	public class SimpleTrace implements IMLBroadcasterListener {
		
		private static var instance:SimpleTrace;
		private var _mc : MovieClip;		
		private var tf:TextField;		
		private var parent:DisplayObjectContainer;		
		private var isVisible:Boolean=false;		
		private var ks:KeyboardService;		private const MAX_HEIGHT:uint=500;		
		private var stage:Stage;
		
		public function SimpleTrace(x:X) {
			stage=MILibMain.getInstance().getStage();
			_mc=MILibMain.getInstance().getMilibRootMCForObj(this);
			parent=_mc.parent;
			tf=new TextField();
			tf.x=tf.y=10;
			tf.width=400;			tf.height=20;
			tf.multiline=true;			tf.wordWrap=true;
			tf.background=true;			tf.selectable=true;
			tf.border = true;			tf.borderColor = 0;
			_mc.addChild(tf);
			parent.removeChild(_mc);
			_trace("SimpleTrace, press '~' + 't' to show/hide");
			ks=KeyboardService.getInstance();
			ks.addListener(this);
		}
		
		public function get mc():MovieClip {
			return _mc;
		}
		
		protected function _trace(text:String):void {
			MILibMain.getInstance().swapMainMCToUp();
			if(!tf.text.length){ setIsVisible(true); }
			tf.appendText('\n '+Math.round(Math.random())+Math.round(Math.random())+Math.round(Math.random())+' '+text);
			renderTextFieldHeight();
		}
		
		protected function renderTextFieldHeight():void {
			if(tf.textHeight+5>stage.stageHeight) {
				tf.height=Math.min(MAX_HEIGHT, stage.stageHeight-20);
			}else if(tf.textHeight+5>MAX_HEIGHT){
				tf.height=MAX_HEIGHT;
			}else{
				tf.height=tf.textHeight+5;
			}
			tf.scrollV=tf.maxScrollV;
			tf.x=stage.stageWidth-tf.width-10;			tf.y=stage.stageHeight-tf.height-10;
		}
		
		public function setIsVisible(visible:Boolean):void {
			if(getTimer()<1000) {
				stage.addEventListener(Event.ENTER_FRAME, onMCEnterFrame);
				return;
			}
			if(isVisible==visible){ return; }
			isVisible=visible;
			if(isVisible){
				stage.addEventListener(Event.RESIZE, onStageResize, false, 0, true);
				parent.addChild(_mc);
				renderTextFieldHeight();
			}else{
				parent.removeChild(_mc);
				stage.removeEventListener(Event.RESIZE, onStageResize);
			}
		}
		
		public static function getInstance():SimpleTrace {
			if(instance){ return instance; }
			else{ instance=new SimpleTrace(null); return instance; }
		}
		
		public static function trace(... text:*):void {
			getInstance()._trace(text.join(', '));
		}
		
//****************************************************************************
// EVENTS for SimpleTrace
//****************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case ks:
					switch(ev.event){
						case ks.event_Down:
							var event:KeyboardEvent=KeyboardEvent(ev.data);
							if(event.keyCode==KeyboardService.KEY_T && ks.getIsKeyDown(KeyboardService.KEY_TILDE)) {
								setIsVisible(!isVisible);
							}
						break;
					}
				break;
			}
		}
		
		protected function onStageResize(event:Event):void {
			renderTextFieldHeight();
		}
		
		protected function onMCEnterFrame(event:Event):void {
			if(getTimer()>1000) {
				stage.removeEventListener(Event.ENTER_FRAME, onMCEnterFrame);
				setIsVisible(true);
			}
		}
		
	}
}

internal class X {}
